package leetcode.all;

import java.util.LinkedList;

public class Solution335 {

    public static void main(String[] args) {

        int[] dis = {3,3,3,2,1,1};
        System.out.println(isSelfCrossing(dis));
    }


    public static boolean isSelfCrossing(int[] distance) {
        //只维护一个长为6的节点队列，每次用最后一个点移动得到新点，如果没有碰撞就删除最旧的点，从0开始计数移动，第0次移动
        LinkedList<long[]> record = new LinkedList<>();
        long x = 0, y = 0;
        record.add(new long[]{x,y});
        //
        for (int i = 0; i < distance.length; i++) {
            switch (i%4){//0上1左2下3右
                case 0:
                    y += distance[i];
                    break;
                case 1:
                    x -= distance[i];
                    break;
                case 2:
                    y -= distance[i];
                    break;
                case 3:
                    x += distance[i];
                    break;
            }
            //此时移动到的点坐标已更新
            if(i>=5){ //5次及以后的移动，新点和01或23碰撞，但是要判断01和23段是纵横
                switch (i%4){ //0上1左2下3右
                    case 0:
                        if(y>=record.get(0)[1]&&record.get(4)[1]<=record.get(0)[1]&&
                                (x>=record.get(0)[0]&&x<=record.get(1)[0]||x>=record.get(1)[0]&&x<=record.get(0)[0])) return true;
                        if(y>=record.get(2)[1]&&record.get(4)[1]<=record.get(2)[1]&&
                                (x>=record.get(3)[0]&&x<=record.get(2)[0]||x>=record.get(2)[0]&&x<=record.get(3)[0])) return true;
                        break;
                    case 1:
                        if(x<=record.get(0)[0]&&record.get(4)[0]>=record.get(0)[0]&&
                                (y>=record.get(0)[1]&&y<=record.get(1)[1]||y>=record.get(1)[1]&&y<=record.get(0)[1])) return true;
                        if(x<=record.get(2)[0]&&record.get(4)[0]>=record.get(2)[0]&&
                                (y>=record.get(3)[1]&&y<=record.get(2)[1]||y>=record.get(2)[1]&&y<=record.get(3)[1])) return true;
                        break;
                    case 2:
                        if(y<=record.get(0)[1]&&record.get(4)[1]>=record.get(0)[1]&&
                                (x>=record.get(0)[0]&&x<=record.get(1)[0]||x>=record.get(1)[0]&&x<=record.get(0)[0])) return true;
                        if(y<=record.get(2)[1]&&record.get(4)[1]>=record.get(2)[1]&&
                                (x>=record.get(3)[0]&&x<=record.get(2)[0]||x>=record.get(2)[0]&&x<=record.get(3)[0])) return true;
                        break;
                    case 3:
                        if(x>=record.get(0)[0]&&record.get(4)[0]<=record.get(0)[0]&&
                                (y>=record.get(0)[1]&&y<=record.get(1)[1]||y>=record.get(1)[1]&&y<=record.get(0)[1])) return true;
                        if(x>=record.get(2)[0]&&record.get(4)[0]<=record.get(2)[0]&&
                                (y>=record.get(3)[1]&&y<=record.get(2)[1]||y>=record.get(2)[1]&&y<=record.get(3)[1])) return true;
                        break;
                }
                //没有碰撞，把新点加入，旧点删除
                record.addLast(new long[]{x,y});
                record.removeFirst();
            }else if(i<=2){ //0，1，2次移动不会碰撞
                record.add(new long[]{x,y});
            }else if(i==3){//3次移动和01段撞
                if(x>=record.get(0)[0]&&y>=record.get(0)[1]&&y<=record.get(1)[1]) return true;
                record.add(new long[]{x,y});
            }else if(i==4){//4次移动和12段或0点重合
                if(x==0&&y>=record.get(0)[1]&&y<=record.get(1)[1]) return true;
                if(y>=record.get(1)[1]&&x<=record.get(1)[0]&&x>=record.get(2)[0]) return true;
                record.add(new long[]{x,y});
            }
        }
        return false;
    }

}
